// src/api/purchaseOrder.ts
import request from '@/utils/request'

export interface PurchaseOrderPayload {
  approvedAt?: string | null
  createdAt: string
  discount: number
  eta: string
  freight: number
  poNo: string
  status: string
  taxRate: number
  vendorId: number
}

export interface PurchaseOrderItem {
  batchNo: string
  expireDate: string
  poId: number
  price: number
  qty: number
  skuId: number
  taxRate: number
}

/**
 * 采购订单列表
 * GET /api/purchase/order/list
 * @returns {Promise<any>} 响应：{"code":0,"message":"success","data":[...]}
 */
export function listPurchaseOrders(): Promise<any> {
  return request({
    url: '/api/purchase/order/list',
    method: 'get',
  })
}

/**
 * 创建采购订单
 * POST /api/purchase/order
 * @param {Object} data
 * @param {string} data.approvedAt
 * @param {string} data.createdAt
 * @param {number} data.discount
 * @param {string} data.eta
 * @param {number} data.freight
 * @param {string} data.poNo
 * @param {string} data.status
 * @param {number} data.taxRate
 * @param {number} data.vendorId
 * @returns {Promise} 响应：{"code":0,"message":"success","data":1}
 */
export function createPurchaseOrder(data: PurchaseOrderPayload): Promise<any> {
  return request({
    url: '/api/purchase/order',
    method: 'post',
    data,
  })
}

/**
 * 添加采购订单明细
 * POST /api/purchase/order/{poId}/items
 * @param {number|string} poId 采购订单ID
 * @param {Array<Object>} items 明细数组
 * @param {string} items[].batchNo
 * @param {string} items[].expireDate
 * @param {number} items[].poId
 * @param {number} items[].price
 * @param {number} items[].qty
 * @param {number} items[].skuId
 * @param {number} items[].taxRate
 * @returns {Promise} 响应：{"code":0,"message":"success","data":1}
 */
export function addPurchaseOrderItems(poId: number | string, items: PurchaseOrderItem[]): Promise<any> {
  return request({
    url: `/api/purchase/order/${poId}/items`,
    method: 'post',
    data: items,
  })
}